package org.mobile.logic;

import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.mobile.beans.Order;
import org.mobile.beans.User;
import org.mobile.database.connection.ConnectionManager;
import org.mobile.database.statements.Statements;
import org.mobile.encryption.MD5;

public class UserLogic {
	
	private static UserLogic instance;
	
	private UserLogic(){
		
	}
	
	public static UserLogic getInstance(){
		if(instance == null){
			instance= new UserLogic();
		}
		return instance;
	}
	
	public User logIn(String login, String password){
		
		Connection conn= null;
		PreparedStatement st= null;
		ResultSet rs= null;
		
		conn= ConnectionManager.getManager().getConnection();
		try {
			st= conn.prepareStatement(Statements.SELECT_USER);
			st.setString(1, login);
			rs= st.executeQuery();
			if(rs.next()){
				if(MD5.encrypt(password).equals(rs.getString("password"))){
					User user= new User();
					user.setLogin(login);
					user.setName(rs.getString("name"));
					user.setSurname(rs.getString("surname"));
					user.setType(rs.getString("type"));
					user.setId(rs.getInt("userID"));
					user.setAddress(rs.getString("address"));
					user.setStyle(rs.getInt("style"));
					user.setPassword(rs.getString("password"));
					return user;
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				rs.close();
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return null;
	}
	
	public boolean addUser(User user){
		
		Connection conn= null;
		PreparedStatement st= null;
		
		
		conn= ConnectionManager.getManager().getConnection();
		try {
			st= conn.prepareStatement(Statements.INSERT_USER);
			st.setString(1, user.getLogin());
			st.setString(2, MD5.encrypt(user.getPassword()));
			st.setString(3, user.getName());
			st.setString(4, user.getSurname());
			st.setString(5, user.getType());
			st.setString(6, user.getAddress());
			st.setInt(7, user.getStyle());
			st.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		finally{
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
	
	public List<User> getUsers(){
		Connection conn= null;
		Statement st= null;
		ResultSet rs= null;
		ArrayList<User> userList= new ArrayList<User>();
		conn= ConnectionManager.getManager().getConnection();
		try {
			st= conn.createStatement();
			rs= st.executeQuery(Statements.SELECT_ALL_USERS);
			while(rs.next()){
				User user= new User();
				user.setLogin(rs.getString("login"));
				user.setName(rs.getString("name"));
				user.setId(rs.getInt("userID"));
				user.setSurname(rs.getString("surname"));
				user.setType(rs.getString("type"));
				user.setAddress(rs.getString("address"));
				userList.add(user);
			}
			return userList;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		finally{
			try {
				st.close();
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	public boolean updateUser(User user){
		Connection conn= null;
		PreparedStatement st= null;

		
		conn= ConnectionManager.getManager().getConnection();
		try {
			st= conn.prepareStatement(Statements.UPDATE_USER);
			st.setString(1, user.getName());
			st.setString(2, user.getSurname());
			st.setString(3, user.getAddress());
			st.setInt(4, user.getId());
			st.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} 
		finally{
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
	
	public boolean removeUser(int userID){
		
		Connection conn= null;
		PreparedStatement st= null;
		conn= ConnectionManager.getManager().getConnection();
		OrderLogic.getInstance().removeOrderByUserID(userID);
		try {
			st= conn.prepareStatement(Statements.REMOVE_USER);
			st.setInt(1, userID);
			st.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		finally{
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	public boolean changeTheme(int userID, int theme){
		Connection conn= null;
		PreparedStatement st= null;
		
		conn= ConnectionManager.getManager().getConnection();
		try {
			st= conn.prepareStatement(Statements.UPDATE_PROFILE);
			st.setInt(1, theme);
			st.setInt(2, userID);
			st.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		finally{
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public boolean changePassword(String password, int userID){
		Connection conn= null;
		PreparedStatement st= null;
		
		conn= ConnectionManager.getManager().getConnection();
		try {
			st= conn.prepareStatement(Statements.UPDATE_USER_PASS);
			st.setString(1, MD5.encrypt(password));
			st.setInt(2, userID);
			st.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		finally{
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public User getUser(int userID){
		Connection conn= null;
		PreparedStatement st= null;
		ResultSet rs= null;
		
		conn= ConnectionManager.getManager().getConnection();
		try {
			st= conn.prepareStatement(Statements.SELECT_USER_BY_ID);
			st.setInt(1, userID);
			rs= st.executeQuery();
			while(rs.next()){
				User user= new User();
				user.setLogin(rs.getString("login"));
				user.setName(rs.getString("name"));
				user.setId(rs.getInt("userID"));
				user.setSurname(rs.getString("surname"));
				user.setType(rs.getString("type"));
				user.setAddress(rs.getString("address"));
				user.setPassword(rs.getString("password"));
				return user;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		finally{
			try {
				rs.close();
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return null;
	}

}
